<?php

include "classes/characteristics.class.php";
include "classes/viability.class.php";

/*
{
    "characteristics": {
        "1": {
            "denomination"  : "blah",
            "categorie"     : "blah",
            "dimension"     : "blah",
            "weight"        :  4,
            "type"          : "asdf",
            "nature"        : "adsf",
            "threshold"     : "asdf"
            }
    }
}
*/



function getCharacteristics(){
    global $CHARACTERISTICS;

    echo "{\n";
    echo "\t\"characteristics\": { \n";

    echo sprintf("\t\t\"length\": %d,\n",  count($CHARACTERISTICS));

    $isTheFirst = True;

    foreach ($CHARACTERISTICS as &$c){

        if (!$isTheFirst)
            echo ",";
        
        $isTheFirst = False;
            

        echo sprintf("\t\t\"%d\": {\n",  $c->id);
        echo sprintf("\t\t\t\"id\":\"%s\",\n",            $c->id);
        echo sprintf("\t\t\t\"denomination\":\"%s\",\n",  $c->denomination);
        echo sprintf("\t\t\t\"categorie\":\"%s\",\n",     $c->categorie);
        echo sprintf("\t\t\t\"dimension\":\"%s\",\n",     $c->dimension);
        echo sprintf("\t\t\t\"weight\":\"%s\",\n",        $c->weight);
        echo sprintf("\t\t\t\"type\":\"%s\",\n",          $c->type);
        echo sprintf("\t\t\t\"nature\":\"%s\",\n",        $c->nature);
        echo sprintf("\t\t\t\"threshold\":\"%s\"\n",      $c->threshold);
            echo "\t\t}\n";
    }
    echo "\t}\n";
    echo "}\n";

}

function getViability(){

    echo "{\n";
    echo "\t\"viability\": { \n";


    try {
        $values = json_decode($_POST["values"]);
        $viability = new Viability($values);


        $plausibility  = $viability->getPlausibility();
        $fitness       = $viability->getFitness();
        $justification = $viability->getJustification();
        $success       = $viability->getSuccess();
        $finalResult   = $viability->getFinalResult();
        $finalResultAvg= $viability->getFinalResultAvg();


        echo sprintf("\t\t\t\"plausibility\":\"%s\",\n" , $plausibility);
        echo sprintf("\t\t\t\"fitness\":\"%s\",\n"      , $fitness);
        echo sprintf("\t\t\t\"justification\":\"%s\",\n", $justification);
        echo sprintf("\t\t\t\"success\":\"%s\",\n"      , $success);
        echo sprintf("\t\t\t\"final_result\":\"%s\",\n"      , $finalResult);
        echo sprintf("\t\t\t\"final_result_avg\":\"%s\"\n"      , $finalResultAvg);



    }
    catch (Exception $e) {
        echo sprintf("\t\t\t\"error\":\"true\",\n");
        echo sprintf("\t\t\t\"error_desc\":\"%s\"\n",  $e->getMessage() );
    }


    echo "\t}\n";
    echo "}\n";


}




$action = $_POST["action"];

if ($action == "getCharacteristics") {
    getCharacteristics();

} 
else if ($action == "getViability") {

    getViability();
}


?>
